package com.whub507.msgmanager.sso.controller;

import com.whub507.msgmanager.common.ResResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@CrossOrigin
@RequestMapping("/logout2")
public class LogoutController {

	/**
	 * 统一认证注销地址
	 */
	@Value("${drap_platform.sso_server.rootpath_proxy}/auth/logout4sso")
	private String logout_url_sso;

	/**
	 * 应用登录入口,记得要追加app_id，便于用户再次登录时，SSO Server做检查和跟踪
	 */
	private String login_url_app = "http://localhost:8080/login";

	/**
	 * 应用唯一标识,对应统一权限分系统中应用ID
	 */
	private String app_id = "app_demo_id";

	@GetMapping
	public ResResult<Boolean> logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 清除会话
		try {

			request.getSession().invalidate();

			return ResResult.ok(Boolean.TRUE, "登出成功");
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		// 跳转到统一认证注销，加上redirect_url参数，用户再次登录还可以跳回应用
		// sso_way=sso_code 标记ssoserver 以一次性认证码方式重定向给redirect_url
		// 保证redirect_url完整性，需要encode
		return ResResult.error(509, "登出失败");
	}

}
